const router = require("koa-router")();
const controller = require("../controller/goods");
const loginCheck = require("../middleware/loginCheck");

// 频道前缀
router.prefix("/goods");

// async function handleGoodsRequest(req, res) {
//     let id = null;
//     let page = null;
//     let postedBody = null;
//     let result = null;

//     try {
//         switch (true) {
//             case req.url === "/goods/0" && req.method === "POST":
//                 postedBody = await getPostedBody(req);
//                 result = await controller.addGoods(postedBody);
//                 res.end(JSON.stringify(result));
//                 break;

//             case reId.test(req.url) && req.method === "DELETE":
//                 /* 登录校验 */
//                 if (!loginCheck(req)) {
//                     res.end(
//                         JSON.stringify({
//                             msg: "删除商品失败",
//                             reason: "请先登录",
//                         })
//                     );
//                     return;
//                 }

//                 id = getPathParams(req.url, reId);
//                 result = await controller.deleteGoods(id);
//                 res.end(JSON.stringify(result));
//                 break;

//             case reId.test(req.url) && req.method === "PUT":
//                 id = getPathParams(req.url, reId);
//                 postedBody = await getPostedBody(req);
//                 console.log("views/goods/put:postedBody", postedBody);
//                 result = await controller.updateGoods(id, postedBody);
//                 res.end(JSON.stringify(result));
//                 break;

//             case req.url === "/goods/list/0" && req.method === "GET":
//                 result = await controller.getAllGoods();
//                 res.end(JSON.stringify(result));
//                 break;

//             case rePage.test(req.url) && req.method === "GET":
//                 page = getPathParams(req.url, rePage);
//                 result = await controller.getGoodsByPage(page);
//                 res.end(JSON.stringify(result));
//                 break;

//             default:
//                 handleError(req, res, errTypes.pageNotFound);
//                 break;
//         }
//     } catch (err) {
//         console.log("err views/goods", err);
//         handleError(req, res, { msg: "服务端错误", err: err.toString() });
//     }
// }

/* 添加商品 */
router.post("/0", loginCheck, async function (ctx, next) {
    result = await controller.addGoods(ctx.request.body);
    ctx.body = result;
});

/* 
删除频道 
DELETE /goods/1234567890
id是动态路由
*/
router.delete("/:id", loginCheck, async function (ctx, next) {
    // 得到动态路由id
    const { id } = ctx.params;
    console.log("views/goods/delete:id=", id);
    result = await controller.deleteGoods(id);
    ctx.body = result;
});

/* 修改商品 */
router.put("/:id", loginCheck, async function (ctx, next) {
    const { id } = ctx.params;
    result = await controller.updateGoods(id, ctx.request.body);
    ctx.body = result;
});

/* 查询所有频道 */
router.get("/list/0", async function (ctx, next) {
    ctx.body = await controller.getAllGoods();
});

module.exports = router;
